<?php
namespace app\flash\controller;
use think\Exception;


class Kline 
{
    public function getk(){
        $symbol = input('symbol');
        $codes = $symbol.'_usdt';
        $code = input('code');
        $type = input('type');//1
        $datatype = input('datatype');
        $res = \cache($symbol.$type.$datatype);
        if ($res) {
            return json(msg(1,['kline'=>$res,'type'=>1],'数据返回成功!'));
        }else{
            $result = \cache($code.$type.$datatype);
            if ($result) {   
                return json(msg(1,['kline'=>$result,'type'=>2],'数据返回成功'));
            }
        }
        if(empty($code)) return json(msg(0,'','请提交完整参数!'));
        $a = '';
        $b='1d';
        if ($datatype==1) {
           $a = $type;
        }else if ($datatype==2) {
            if ($type == '1d') {
                $b = 'd';
            }
            if ($type == '7d') {
                $b = 'w';
            }
            if ($type == '1m') {
                $b = 'm';
            }
            if ($type == '6m') {
                $b = $type;
            }
            if ($type == '1y') {
                $b ='y';
            }
        }
        // $a = '';
        // $b = 'all';
        // if (is_integer($type)) {
        //     $a = $type.'min';
        //     $b = '1d';
        // }
        // if (is_string($type)) {
        //     $strlen = strlen($type);
        //     $str = substr($type,-1,1);
        //     $time ='';
        //     if ($strlen==3) {
        //         $time = substr($type,0,2);
        //     }else{
        //         $time = substr($type,0,1);
        //     }
        //     if ($str=='d') {
        //         $a = $time.'day';
        //         $b = $time.'d';
        //     }
        //     if ($str == 'h') {
        //         $a = $time.'hour';
        //         $b = $time.'h';
        //     }
        // };
        $opts=array(
            "http"=>array(
                "method"=>"GET",
                "timeout"=>3,
                'header' => "User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
            ),
        );
        $context = stream_context_create($opts);
        $res = @file_get_contents("http://api.zb.cn/data/v1/kline?market=$codes&type=$a",false, $context);
        $arr = \json_decode($res,true);
        if (!empty($arr['data'])) {
            cache($symbol. $type.$datatype,$arr['data'],120);
            return json(msg(1,['kline'=>$arr['data'],'type'=>1],'数据返回成功'));
        }else{
            $res = file_get_contents("https://dncapi.bqiapp.com/api/coin/web-charts?code=$code&webp=1&type=$b",false, $context);
            // $aa = "https://dncapi.bqiapp.com/api/coin/web-charts?code=$code&webp=1&type=$b";
            $arr = json_decode($res,true);
            if(isset($arr['code']) && $arr['code']== 200 ){
                $size = 2;
                 cache($code. $type.$size,$arr['value'],120);
                return json(msg(1,['kline'=>$arr['value'],'type'=>2],'数据返回成功!'));
            }else{
                return json(msg(0,'','暂无数据!'));
            }
        }
    }
}
